<?php
	/*
	*	File:	MiaoAction.class.php
	*	Date:	2013-4-26 14:10
	*	喵星人星球操作类
	*	@Wang Xiaohui
	*/
	
class MiaoAction extends Action {
	
	//type定义获取类型
	private $type = 'miao';
	
    public function index(){
    	//获取菜单
    	$this->getMuen();
    	
    	//获取数据
    	$Miao = M('goods');
    	
    	//分页功能
		import("ORG.Util.Page");// 导入分页类
		$count      = $Miao->where("`type`='".$this->type."'")->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('theme','共有%totalRow%%header% 第%nowPage%页/共%totalPage%页 %first%%upPage%%linkPage%%downPage%%end%');
		//获取数据
		$data = $Miao->field('id,title,collection,ctime')->where("`type`='".$this->type."'")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$Page->setConfig('first','<<第一页');
		$Page->setConfig('last','最后一页>>');
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('data',$data);
		
    	$this->display();
    }
    
    public function add(){
	    //获取菜单
	    $this->getMuen();
	    
	    $this->display();
    }
    
    public function addAction(){
    	$this->getMuen();
    	
	    $title = $this->_post('title');
	    $price = $this->_post('price');
	    $paper = $this->_post('paper');
	    $collection = $this->_post('collection');
	    $buy_url = $this->_post('buy_url');
	    $content = $this->_post('content');
	    
	    //上传图片
	    import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Uploads/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
	    
	    if(empty($title) || empty($paper) || empty($collection) || empty($buy_url) || empty($content)){
		    $this->error('所有数据不能为空');
		    return;
	    }
	    
	    if(!preg_match("/^\d*$/",$collection)){
		    $this->error('收藏数不为数字');
		    return;
	    }
	    
	    $data = array(
	    	'title' => $title,
	    	'price' => $price,
	    	'paper' => $paper,
	    	'collection' => $collection,
	    	'buy_url' => $buy_url,
	    	'content' => $content,
	    	'type' => $this->type,
	    	'img' => $info[0]['savename'],
	    	'ctime' => time(),
	    );
	    
	    $Miao = M('goods');
	    
	    
	    if($Miao->add($data)){
		    $this->success('添加商品成功', '__URL__/index');
	    }else{
		    $this->error('添加商品失败');
	    }
	    
    }
    
    public function edit(){
     	$this->getMuen();
     	
    	$id = $this->_get('id');
    	
    	$Miao = M('goods');
    	
    	$Data = $Miao->where('id = '.$id)->find();
    	
    	$this->assign('data', $Data);
    	
    	$this->display();
    	
    }
    
    public function editAction(){
    	$this->getMuen();
    	
    	$title = $this->_post('title');
	    $paper = $this->_post('paper');
	    $price = $this->_post('price');
	    $collection = $this->_post('collection');
	    $buy_url = $this->_post('buy_url');
	    $content = $this->_post('content');
	    $id = $this->_get('id');
	    
	    //上传图片
	    import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Uploads/';// 设置附件上传目录
		
	    
	    if(empty($title) || empty($paper) || empty($collection) || empty($buy_url) || empty($content)){
		    $this->error('所有数据不能为空');
		    return;
	    }
	    
	    if(!preg_match("/^\d*$/",$collection)){
		    $this->error('收藏数不为数字');
		    return;
	    }
	    
	    $data = array(
	    	'title' => $title,
	    	'price' => $price,
	    	'paper' => $paper,
	    	'collection' => $collection,
	    	'buy_url' => $buy_url,
	    	'content' => $content,
	    );
	    
	    if($upload->upload()) {// 上传错误提示错误信息
			$info =  $upload->getUploadFileInfo();
			
			$data['img'] = $info[0]['savename'];
		}
	    
	    $Miao = M('goods');
	    
    	if($Miao->where('id = '.$id)->save($data)){
	    	$this->success('修改数据成功', '__URL__/index');
    	}else{
    		$this->error('修改数据失败');
    	}
    }
    
    public function delete(){
    	$this->getMuen();
    	
    	$id = $this->_get('id');
    	
    	$Miao = M('goods');
    	
    	
	    if($Miao->where('id='.$id)->delete()){
			$this->success('删除商品成功', '__URL__/index');
		}
    }
    
    public function getMuen(){
    	R("Login/isLogin");
    	
	   	$menu = include(COMMON_PATH.'menu.php');
		$this->assign('menu', $menu);
    }
}